package com.ease.platform.concurrent.service.impl;

import com.ease.concurrent.ConcurrentRetailCallback;
import com.ease.concurrent.ConcurrentRetailExecutor;
import com.ease.concurrent.ConcurrentThreadContextPassCallback;
import com.ease.platform.authorization.service.impl.AbstractAuthorizationServiceImpl;
import com.ease.platform.concurrent.service.AbstractConcurrentRetailExecutorService;

public abstract class AbstractConcurrentRetailExecutorServiceImpl extends AbstractAuthorizationServiceImpl implements AbstractConcurrentRetailExecutorService {

	protected ConcurrentRetailExecutor concurrentRetailExecutor;

	public void execute(final ConcurrentRetailCallback<?> concurrentRetailCallback) {
		this.execute(concurrentRetailCallback, null);
	}

	public void execute(final ConcurrentRetailCallback<?> concurrentRetailCallback, final ConcurrentThreadContextPassCallback concurrentContextHandoverCallback) {
		if (null != concurrentContextHandoverCallback) {
			concurrentContextHandoverCallback.onMainThread();
		}

		concurrentRetailExecutor.execute(new Runnable() {

			public void run() {
				if (null != concurrentContextHandoverCallback) {
					concurrentContextHandoverCallback.onForkThread();
				}
				try {
					concurrentRetailCallback.execute();
				} catch (Throwable e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * @return the concurrentRetailExecutor
	 */
	public ConcurrentRetailExecutor getConcurrentRetailExecutor() {
		return concurrentRetailExecutor;
	}

	/**
	 * @param concurrentRetailExecutor
	 *            the concurrentRetailExecutor to set
	 */
	public void setConcurrentRetailExecutor(ConcurrentRetailExecutor concurrentRetailExecutor) {
		this.concurrentRetailExecutor = concurrentRetailExecutor;
	}

}
